Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring - Constructor Injection using XML

Constructor Injection is almost same as Setter Injection only with minor changes.

Say there is a class called 'Car' and since we are dealing with constructor injection, we need to define a constructor with an argument:


class Car {

  String brand;
  String type;

  public Car() {

  }

  public Car(String brand, String type) {

   this.brand = brand;
   this.type = type;
  }

}


So, we have defined a one argument constructor to see how constructor injection works:


public Car(String brand, String type) {

   this.brand = brand;
   this.type = type;
}

Also let us define the application-context.xml



application-context.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation = "http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean name = "car" class = "Car">
   <constructor-arg index="0" value="Hyundai">
   <constructor-arg index="1" value="Sedan">
  </bean>

</beans >

So, this time we have used <constructor-arg..> tag to tell spring, we are going to inject values through constructor.


<bean name = "car" class = "Car">
   <constructor-arg index="0" value="Hyundai">
   <constructor-arg index="1" value="Sedan">
</bean>

Just note we have used index values as '0' and '1'. Which means the value of 0th index will be assigned to first parameter and 1st index to second parameter of the constructor.


public Car(String brand, String type)

So, the 'brand' will be assigned with 'Hyundai'(0th index).

And, 'type' will be assigned with 'Sedan'(1st index).


A Complex Constructor Injection

In the below code we will be writing a constructor in the Car class, which takes the 'Wheel' as a paramenter and initializes it.


Car Class

class Car {

 Wheel wheel;

 public Car() {

 }

 public Car(Wheel wheel){

   this.wheel = wheel;
 }
}

Wheel Class

class Wheel {

 String tireType;

 public String getTireType() {
   return tireType;
 }

 public void setTireType(String tireType) {
   this.tireType = tireType;
 }

 }

Now, since the 'Car' class is holding a reference of 'Wheel'. We will define them in 'application-context.xml'.


application-context.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation = "http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean name = "car" class = "Car">
   <constructor-arg index="0" ref="wheelReference">
  </bean>

  <bean name = "wheelReference" class = "Wheel">
   <property name = "wheel" value="Thick Tire"/>
  </bean>

</beans >

Inside the 'Car' <bean name =...>, we have placed the <constructor-arg ..> tag


<bean name = "car" class = "Car">
   <constructor-arg index="0" ref="wheelReference">
</bean>

Which means Spring has to search the bean with the reference (i.e. ref="wheelReference") and once found (mentioned below)


<bean name = "wheelReference" class = "Wheel">

it has to inject the 'Wheel' object to the constructor of the 'Car' class.


public Car(Wheel wheel) {

  this.wheel = wheel;
}